package org.lq.er.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.lq.er.dao.StudentDao;
import org.lq.er.entity.Student;
import org.lq.er.util.Instantiation;
import org.lq.er.util.JDBCUtil;
/**
 * 学生数据访问接口实现类
 * @author 宋铀
 * 
 * @package org.lq.er.dao.impl
 * @date 2020年8月24日
 */
public class StudentDaoImpl implements StudentDao,Instantiation<Student> {
	
	/**
	 * 添加学生信息
	 */
	@Override
	public int save(Student t) {
		// 插入	sql语句
		String sql = "insert into student(card_id,name,password,phone_no,classes,stu_no) values (?,?,?,?,?,?)";
		// SQL语句执行结果
		int flag = 0;
		try {
			flag = JDBCUtil.executeUpdate(sql, t.getCard_id(),t.getName(),t.getPassword(),t.getPhone_no(),t.getClasses(),t.getStu_no());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}
	/**
	 * 修改学生信息
	 */
	@Override
	public int update(Student t) {
		// 修改	sql语句
		String sql = "update student set password=?,phone_no=?,classes=?,stu_no=? where card_id = ?";
		// sql语句执行结果
		int flag = 0;
		try {
			flag = JDBCUtil.executeUpdate(sql, t.getPassword(),t.getPhone_no(),t.getClasses(),t.getStu_no(),t.getCard_id());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}
	/**
	 * 根据学号删除
	 */
	@Override
	public int delete(int id) {
		// 删除	sql语句
		String sql = "delete from student where stu_no = ?";
		// sql语句执行结果
		int flag = 0;
		try {
			flag = JDBCUtil.executeUpdate(sql, id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}
	/**
	 * 根据身份证号删除
	 * @param card_id
	 * @return
	 */
	@Override
	public int deleteByCard_id(String card_id) {
		// 删除	sql语句
		String sql = "delete from student where card_id = ?";
		// sql语句执行结果
		int flag = 0;
		try {
			flag = JDBCUtil.executeUpdate(sql, card_id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}
	/**
	 * 查询所有
	 */
	@Override
	public List<Student> findAll() {
		// 查询结果
		List<Student> list = new ArrayList<Student>(); 
		// 删除	sql语句
		String sql = "select * from student";
		try {
			list = JDBCUtil.executeQuery(sql, this);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 根据学号查询
	 */
	@Override
	public Student getById(int id) {
		// 查询结果
		List<Student> list = new ArrayList<Student>();
		// 创建学生实体类
		Student student = null;
		// 查询	SQL语句
		String sql = "select * from student where stu_no=?";
		try {
			list = JDBCUtil.executeQuery(sql, this,id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (!list.isEmpty()) {			
			student = list.get(0);
		} else {
			student = null;
		}
		return student;
	}
	/**
	 * 根据身份证查询
	 */
	@Override
	public Student getByCard_id(String id) {
		// 查询结果
		List<Student> list = new ArrayList<Student>();
		// 创建学生实体类
		Student student = null;
		// 查询	SQL语句
		String sql = "select * from student where card_id=?";
		try {
			list = JDBCUtil.executeQuery(sql, this,id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (!list.isEmpty()) {			
			student = list.get(0);
		} else {
			student = null;
		}
		return student;
	}
	
	@Override
	public Student instance(ResultSet rs) {
		// 创建一个学生实体类
		Student student = new Student();
		try {
			student.setCard_id(rs.getString("card_id"));
			student.setName(rs.getString("name"));
			student.setPassword(rs.getString("password"));
			student.setPhone_no(rs.getString("phone_no"));
			student.setClasses(rs.getString("classes"));
			student.setStu_no(rs.getInt("stu_no"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return student;
	}

	
	

	/**
	 * 校验身份
	 */
	@Override
	public Student check(String cardId, String passwd) {
		// 查询结果
		List<Student> list = new ArrayList<Student>();
		// 创建学生实体类
		Student student = null;
		// 查询	SQL语句
		String sql = "select * from student where card_id=? and password = ?";
		try {
			list = JDBCUtil.executeQuery(sql, this,cardId,passwd);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (!list.isEmpty()) {			
			student = list.get(0);
		} else {
			student = null;
		}
		return student;
	}
	/**
	 * 根据身份证号码/姓名/手机号查询/班级查询
	 */
	@Override
	public List<Student> getByItem(String item) {
		// 查询结果
		List<Student> list = new ArrayList<Student>();
		// 创建学生实体类
		Student student = null;
		// 查询	SQL语句
		String sql = "select * from student where card_id like ? or name like ? or phone_no like ? or classes like ?";
		try {
			list = JDBCUtil.executeQuery(sql, this,"%"+item+"%","%"+item+"%","%"+item+"%","%"+item+"%");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}


}
